Ext.define('MdxDefine.views.MdxDefineMainPanel', {
	extend : 'Ext.Viewport',
	requires : [ 'MdxDefine.views.MdxGridPanel',
			'MdxDefine.views.CubeMetaPanel', 'MdxDefine.views.MdxChartPanel',
			'MdxDefine.views.MdxStringPanel' ],
	layout : {
		type : 'border',
		padding : 17
	},
	autoScroll : true,
	bodyStyle : "overflow-x:hidden;overflow-y:auto",
	defaults : {
		split : true
	},
	id : 'mainPanel',
	items : [ {
		region : 'center',
		minHeight : 1000,
		items : [ {
			xtype : 'MdxGridPanel',
			region : 'north',
			collapsible : true,
			title : '数据区',
			split : true,
			height : 330,
			minHeight : 330
		}, {
			xtype : 'MdxStringPanel',
			region : 'center',
			collapsible : true,
			title : '生成的mdx',
			split : true,
			height : 150,
			minHeight : 150
		}, {
			xtype : 'MdxChartPanel',
			region : 'south',
			collapsible : true,
			title : '预览图',
			split : true,
			height : 500,
			minHeight : 500
		} ]
	}, {
		region : 'east',
		split : true,
		width : 280,
		minWidth : 280,
		height : 600,
		minHeight : 600,
		layout : {
			type : 'vbox',
			align : 'stretch'
		},
		items : [ {
			xtype : 'CubeMetaPanel',
			region : 'north',
			collapsible : true,
			title : 'CUBE元数据',
			split : true
		} ]
	} ],
	generateMdx : function() {
		var cubeName = Ext.getCmp('cubeName').value;
		// 报表筛选
		var _grid1_data = Ext.getCmp('grid1').view.dataSource.data.items;
		// 列标签
		var _grid2_data = Ext.getCmp('grid2').view.dataSource.data.items;
		// 行标签
		var _grid3_data = Ext.getCmp('grid3').view.dataSource.data.items;
		// 数值
		var _grid4_data = Ext.getCmp('grid4').view.dataSource.data.items;

		var mdxStr = "select ";
		var columns = "";
		var rows = "";
		var measures = "";
		for ( var i = 0; i < _grid2_data.length; i++) {
			var title = _grid2_data[i].data.id;
			mdxStr += (0 == i ? " NON EMPTY {" : "*") + title;
			mdxStr += 1 + i == _grid2_data.length ? " } on columns \r\n" : '';
		}

		for ( var i = 0; i < _grid3_data.length; i++) {
			var title = _grid3_data[i].data.id;
			mdxStr += (0 == i ? ", NON EMPTY {" : "*") + title;
			if (1 + i == _grid3_data.length) {
				mdxStr += " } on rows \r\n";
			}
		}
		mdxStr += " from " + cubeName + " \r\n";

		for ( var i = 0; i < _grid4_data.length; i++) {
			var title = _grid4_data[i].data.id;
			mdxStr += " where " + title;
		}
		Ext.getCmp('generatedMdx').setValue(mdxStr);
		this.query(mdxStr);
	},
	query : function(mdxStr) {
		Ext.Ajax
				.request({
					url : '/mdx/MdxCtrl',
					params : {
						mdxStr : mdxStr
					},
					success : function(response) {
						Ext.getCmp('mdxGridPanel').loadResultSet(
								response.responseText);
					},
					error : function(response) {

					}
				});
	},
});
